package com.nuanshui.heatedloan.order.integration.rabbitmq;

import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.nuanshui.heatedloan.order.integration.execute.OrderExecuteFacade;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 预约订单, 到期未入场订单失效的消息处理(展馆预约 电影预约, 活动预约之类的预约 都会到期)
 * @Author: Gonzo
 * @Date 2017/4/12下午7:22
 * @since 1.4.2
 */
@MqMessageHandler(value = RabbitMqQueue.RABBIT_MQ_MUSEUM_APPOINTMENT_EXPIRE)
public class MuseumAppointmentOrderExpireHandler implements RabbitmqMessageHandler {

    private static Logger logger = LoggerFactory.getLogger("appointment_order");

    @Autowired
    private OrderExecuteFacade orderExecute;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try{
            LogUtil.info(logger, "---------------------开始执行 <预约订单> <到期未入场> 自动失效-----------");
            byte[] body = message.getBody();
            String orderNum = new String(body);
            LogUtil.info(logger, String.format("---------------------orderNum : %s-----------", orderNum));
            if (orderNum.endsWith("\"")) {
                orderNum = orderNum.substring(1, orderNum.length() - 1);
            }
            orderExecute.expireOrder(orderNum);

            LogUtil.info(logger, "---------------------结束执行 <预约订单> <到期未入场> 自动失效  成功-----------");
        }catch(Exception e){
            logger.error(String.format("执行<预约订单> <到期未入场>, 失效失败! %s", message.getBody()), e);
            //channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            return;
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
